//
//  MYGAddTopicViewController.swift
//  MYDoctor
//
//  Created by Denny on 2024/4/18.
//

import UIKit

class MYGAddTopicViewController: MYGBaseController {
    @IBOutlet weak var descTF: UITextView!
    @IBOutlet weak var contentTFHeight: NSLayoutConstraint!
    @IBOutlet weak var descHeight: NSLayoutConstraint!
    @IBOutlet weak var sureBtn: UIButton!
    @IBOutlet weak var contentTF: UITextView!
    @IBOutlet weak var titileTF: UITextView!
    
    @IBOutlet weak var descLimitLB: UILabel!
    
    @IBOutlet weak var conetntLimitLB: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "发布话题"
        self.titileTF.placeholderText = "请输入标题"
        self.descTF.placeholderText = "请输入简介"
        self.contentTF.placeholderText = "请输入内容"
        sureBtn.addCAGradientLayerColor(colors: [UIColor.init(hexString: "#97B3FF").cgColor,
                                                 UIColor.init(hexString: "#4975EC").cgColor],
                                        startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint.init(x: 1, y: 0))
    }

    @IBAction func tapAction(_ sender: UIButton) {
        if self.titileTF.text.count == 0 {return}
        if self.descTF.text.count == 0 {return}
        if self.contentTF.text.count == 0 {return}
        MYGLoading.show()
        MYGNetwork<MYGMeAPI, MYGEmptyModel>.request(target: .addTopic(param: [
            "title":self.titileTF.text ?? "",
            "desc":self.descTF.text ?? "",
            "content":self.contentTF.text ?? ""
        ])) { item in
            NotificationCenter.default.post(name: Notification.Name("MYGTopicViewController_refresh"), object: nil)
            self.navigationController?.popViewController(animated: true)
       } failed: { _, error in
           MYGLoading.hide()
           MMYGHUD.showToast(text: error)
       }
    }
}

extension MYGAddTopicViewController:UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        var maxLength:Int = 99
        if textView == self.contentTF {
            maxLength = 999
        }
        if textView.text.count > maxLength {
            textView.text = String(textView.text.prefix(maxLength))
        }
        if textView == self.contentTF {
            self.conetntLimitLB.text = "\(textView.text.count)" + "/" + "\(maxLength)"
        }else {
            self.descLimitLB.text = "\(textView.text.count)" + "/" + "\(maxLength)"
            
        }
    }
}
